<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter Datagrid Class
 *
 * This class enables the creation of datagrid
 *
 * @package		CodeIgniter
 * @subpackage	Libraries
 * @category	Libraries
 * @author		locdo@penwoodtech.com
 */

	class Datagrid{
		# public properties
		var $id				= '';
		var $form			= 'frm';
		var $classname 		= 'datagrid';	
		var $page 			= 1;
		var $pages 			= 1;
		var $siteurl		= '';
		var $addnewmethod	= 'addnew';
		var $deletemethod	= 'delete';
		var $updatemethod	= 'update';
		var $savemethod		= 'save';		
		var $enableadd		= true;
		var $enabledelete	= true;
		var $enablesave		= true;
		var $enablepaging 	= true;
		# private properties	
		var $_datasource	= array();	
		var $_columns 		= array();		
		var $_numrows		= 0;	
		# url 
		var $url           = '';	
		
		function Datagrid(){
			$this->setColumn('','10%');			
		}			
		function setColumn($title,$width,$align='left',$type=0,$option=''){
			$this->_columns[$title==''?0:count($this->_columns)] = array('title'=>$title,'width'=>$width,'align'=>$align,'type'=>$type,'option'=>$option);
		}		
		function setDataSource($datasource){
			$this->_datasource = $datasource;
			$this->_numrows = count($datasource);
		}
		function _genMenu($id){
			$cbopage = "<select name='{$this->id}_cbo_page{$id}' onchange='{$this->id}_changepage(this)'>";
			for($i=0;$i<$this->pages;$i++) $cbopage.="<option".($this->page==($i+1)?" selected='selected'":"").">".($i+1)."</option>";	
			$cbopage.= "</select>";
			return	"<table cellspacing='0' cellpadding='0' border='0' width='100%'>
				<tr><td width='50%' style='text-align:left'>".
						($this->enableadd?"<input class='button' type='button' name='{$this->id}_btn_addnew{$id}' value='".lang('datagrid_addnew')."' onclick='{$this->id}_addnew()'/>":"").
						($this->enabledelete?"<input class='button' type='button' name='{$this->id}_btn_delete{$id}' value='".lang('datagrid_delete')."' onclick='{$this->id}_multidelete(".$this->_numrows.")'/>":"").
						($this->enablesave?"<input class='button' type='button' name='{$this->id}_btn_save{$id}' value='".lang('datagrid_save')."' onclick='{$this->id}_save()'/>":"").
					"</td>
					<td width='50%' style='text-align:right'>".($this->enablepaging&&$this->pages>1?lang('datagrid_page')." {$cbopage}/{$this->pages}":"")."</td></tr></table>";			
		}
		function render(){
			$w0 = explode('%',$this->_columns[0]['width']);				
			if(count($w0)==2){ $w = $w0[0]; $u = '%';}
			else{
				$w0 = explode('px',$this->_columns[0]['width']);
				$w 	= count($w0)==2?$w0[0]:''; $u = 'px';
			}
			$w1 = $w2 = '';
			if(!empty($w)){
				$w1 = intval($w/4).$u;
				$w2 = $w-3*intval($w/4).$u;
			}			
			echo $this->_javascript();			
			echo "<input type='hidden' id='{$this->id}_hidden_value'  name='{$this->id}_hidden_value' value=''/>";
			echo "<input type='hidden' id='{$this->id}_hidden_page'  name='{$this->id}_hidden_page' value='{$this->page}'/>";
			echo "<table class='{$this->classname}' cellspacing='1' cellpadding='2' border='0' width='100%'>";
			echo "<tr><td class='dtgrid_menu' colspan='".(count($this->_columns)+3)."'>".$this->_genMenu(1)."</td></tr>";
			echo "<tr>";
			echo "<th colspan='4' width='".$this->_columns[0]['width']."' style='text-align:center'>
				  <input type='checkbox' id='{$this->id}_chk_checkall' onclick='{$this->id}_checkall(this,".$this->_numrows.")'/></th>";
			for($i=1;$i<count($this->_columns);$i++){ 			
				echo "<th width='".$this->_columns[$i]['width']."'>".$this->_columns[$i]['title']."</th>"; 
			}
			echo "</tr>";			
			
			for($i=0;$i<$this->_numrows;$i++){
				$r = $this->_datasource[$i];
				$itemstyle=$i%2==0?'itemstyle1':'itemstyle2';					
				echo "<tr class='{$itemstyle}' onmouseover='this.className=\"itemstyle3\"' onmouseout='this.className=\"{$itemstyle}\"'>";
				echo "<td width='{$w1}' style='text-align:center'>
						<input type='checkbox' id='{$this->id}_chk_select_{$i}' name='{$this->id}_chk_select[]' 
						value='{$r[0]}' onclick='{$this->id}_checkrow(this,".$this->_numrows.")' /></td>";
				echo "<td width='{$w1}' style='text-align:center'>
						<a href='javascript:{$this->id}_update({$r[0]})'><img src='".base_url()."media/admin/images/btnedit.png' border='0' title='".lang('datagrid_update')."'></a></td>";
				echo "<td width='{$w1}' style='text-align:center'>
						<a href='javascript:{$this->id}_delete({$r[0]})'><img src='".base_url()."media/admin/images/btndelete.png' border='0' title='".lang('datagrid_delete')."'></a></td>";
				echo "<td width='{$w2}' style='text-align:center'>".($i+1)."<input type='hidden' name='{$this->id}_hidden_id[]' value='{$r[0]}' /></td>";					
				for($j=1;$j<count($this->_columns);$j++){
					echo "<td style='text-align:".$this->_columns[$j]['align']."'>";
					switch($this->_columns[$j]['type']){
						case 0: echo htmlspecialchars($r[$j]); break;
						case 1: echo "<input ".$this->_columns[$j]['option']." type='checkbox' name='{$this->id}_column_{$j}[]' value='{$r[0]}' ".($r[$j]=="1"?"checked='checked'":"")." />"; break;
						case 2: echo "<input ".$this->_columns[$j]['option']." type='text' name='{$this->id}_column_{$j}[]' value='".htmlspecialchars($r[$j])."'/>"; break;
						case 3: echo "<img   ".$this->_columns[$j]['option']." src='".$r[$j]."' border='0' >"; break;
						default: break;
					}						
					echo "</td>";
				}					
				echo "</tr>";
			}
				
			if($this->_numrows==0){
				echo "<tr class='itemstyle1'><td class='msg' colspan='".(count($this->_columns)+3)."'>".lang('datagrid_msg_empty')."</td></tr>";
			}
			echo "<tr><td class='dtgrid_menu' colspan='".(count($this->_columns)+3)."'>".$this->_genMenu(2)."</td></tr>";
			echo "</table>";
		}
		function _javascript(){
			return 
			"<script type='text/javascript'>
				function {$this->id}_checkall(chk,count){ 
					flag = chk.checked?true:false;
					for(i=0;i<count;i++){ eval(\"document.getElementById('{$this->id}_chk_select_\"+i+\"').checked=flag;\"); }
				}				
				function {$this->id}_checkrow(chk,count){
					if(!chk.checked) document.getElementById('{$this->id}_chk_checkall').checked=false;
					else{
						flag = true;
						for(i=0;i<count;i++){
							eval(\"if(!document.getElementById('{$this->id}_chk_select_\"+i+\"').checked)flag=false;\");							
						}
						if(flag) document.getElementById('{$this->id}_chk_checkall').checked=true;
					}
				}
				function {$this->id}_addnew(){ window.location='".$this->siteurl.'/'.$this->addnewmethod."'; }
				function {$this->id}_update(id){ window.location='".$this->siteurl.'/'.$this->updatemethod."/' + id; }
				function {$this->id}_multidelete(count){ 
					flag = false;
					for(i=0;i<count;i++){
						eval(\"if(document.getElementById('{$this->id}_chk_select_\"+i+\"').checked)flag=true;\");	
					}
					if(flag){
						if(confirm('".lang('datagrid_js_multidelete')."')){
							document.{$this->form}.action = '".$this->siteurl.'/'.$this->deletemethod."';
							document.{$this->form}.submit();
						}
					}
					else{
						alert('".lang('datagrid_js_delete_select')."');
					}
				}				
				function {$this->id}_delete(id){ 
					if(confirm('".lang('datagrid_js_delete')."')){
						document.getElementById('{$this->id}_hidden_value').value = id;
						document.{$this->form}.action = '".$this->siteurl.'/'.$this->deletemethod."';
						document.{$this->form}.submit();
					}
				}
				function {$this->id}_save(){
					document.{$this->form}.action = '".$this->siteurl.'/'.$this->savemethod."';
					document.{$this->form}.submit();
				}
				function {$this->id}_changepage(cbo){ 
					document.getElementById('{$this->id}_hidden_page').value = cbo.value;
					document.{$this->form}.action = '".$this->siteurl.$this->url."';
					document.{$this->form}.submit();
				}
			</script>";
		}
	}

/* End of file Datagrid.php */
/* Location: ./system/application/admin/libraries/Datagrid.php */